<template>
  <div>权限分配{{ cuiId }}
    <el-tree ref="tree" :data="list" :props="defaultProps" show-checkbox check-strictly node-key="id"	/>
    <el-row slot="footer" type="flex" justify="center">
      <el-button size="small">取消</el-button>
      <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
    </el-row>
  </div>
</template>

<script>
import { getPermissionList } from '@/api/permissions'
import { getRoleDetail, assignPerm } from '@/api/settings'
export default {
  props: {
    cuiId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      list: [],
      defaultProps: { label: 'name' }
    }
  },
  created() {
    this.getmenus()
    this.getgougou()
  },
  methods: {
    async  hSubmit() {
      const permIds = this.$refs.tree.getCheckedKeys()
      const res = await assignPerm({ id: this.cuiId, permIds })
      console.log(res)
    },
    async  getmenus() {
      const data = await getPermissionList()
      const list = data.data
      // console.log(list)
      const newarr = list.filter(item => {
        item.children = list.filter(ele => { return item.id === ele.pid })
        return item.pid === '0'
      })
      // console.log(newarr)
      this.list = newarr
    },
    async getgougou() {
      const { data: res } = await getRoleDetail(this.cuiId)
      // console.log(res.permIds)
      this.$refs.tree.setCheckedKeys(res.permIds)
    }
  }
}
</script>

<style>

</style>
